import type { Next, Context } from 'koa';
import type { Server as HttpServer } from 'http';
import { withChat } from '@/sockets/chat';
import { withNotify } from '@/sockets/notify';
import { SocketPlus } from '@/libs/socket';

export const socketMiddleware = (server: HttpServer) => {
  const socket = new SocketPlus(server);
  socket.add(withChat).add(withNotify).start();
  return async (ctx: Context, next: Next) => {
    ctx.socketIo = socket.nsp;
    await next();
  }
}